package com.hboxs.asl.controller.app.product;

import com.hboxs.asl.Filter;
import com.hboxs.asl.JsonMessage;
import com.hboxs.asl.Page;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.controller.app.BaseController;
import com.hboxs.asl.entity.Category;
import com.hboxs.asl.entity.Course;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.service.CategoryService;
import com.hboxs.asl.service.CourseService;
import com.hboxs.asl.service.MemberService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller(value = "appCourseController")
@RequestMapping(value = "/app/course")
public class CourseControl extends BaseController {

    @Resource(name = "courseServiceImpl")
    private CourseService courseService;
    @Resource(name = "memberServiceImpl")
    private MemberService memberService;
    @Resource(name = "categoryServiceImpl")
    private CategoryService categoryService;

    /**
     * 课程/预告详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public
    @ResponseBody
    JsonMessage detail(Long id) {
        Course course = courseService.find(id);
        if(course == null) {
            return JsonMessage.error("找不到该课程/预告");
        }
        return JsonMessage.success(JsonMessage.optimiseObj(course, "title", "createDate", "id", "introduction", "imageURL"));
    }

    /**
     * 课程列表
     * @param pageable
     * @return
     */
    @RequestMapping(value = "/courseList", method = RequestMethod.POST)
    public
    @ResponseBody
    JsonMessage courseList(Pageable pageable) {
        Page<Course> page = courseService.findPageGroup(Course.Type.course, pageable);
        if(page.getContent().size() > 0) {
            return JsonMessage.success(
                    "totalPage", page.getTotalPages(),
                    "pageNumber", page.getPageNumber(),
                    "pageSize", page.getPageSize(),
                    "total", page.getTotal(),
                    "content", JsonMessage.optimiseObj(page.getContent(), "title", "id", "introduction", "imageURL", "createDateF", "pubdate", "encrypt", "password"));
        } else {
            return JsonMessage.error("找不到课程");
        }
    }

    /**
     * 预告列表
     * @param pageable
     * @return
     */
    @RequestMapping(value = "/trailerList", method = RequestMethod.POST)
    public
    @ResponseBody
    JsonMessage trailerList(Pageable pageable) {
        Page<Course> page = courseService.findPageGroup(Course.Type.trailer, pageable);
        return JsonMessage.success(
                    "totalPage", page.getTotalPages(),
                    "pageNumber", page.getPageNumber(),
                    "pageSize", page.getPageSize(),
                    "total", page.getTotal(),
                    "content", JsonMessage.optimiseObj(page.getContent(), "title", "id", "introduction", "imageURL", "createDate", "pubdate"));
    }

    /**
     * 预告列表(老师id)
     * @param teacherId
     * @param pageable
     * @return
     */
    @RequestMapping(value = "/trailer_list", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage trailer_list(Long teacherId, Pageable pageable) {
        try {
            Member teacher = memberService.find(teacherId);
            if(teacher == null) {
                return JsonMessage.error("找不到该老师");
            }
            Page<Course> page = courseService.findVedioPageByTeacherId(pageable, teacher, Course.Type.trailer);
            return JsonMessage.success(
                    "totalPage", page.getTotalPages(),
                    "pageNumber", page.getPageNumber(),
                    "pageSize", page.getPageSize(),
                    "total", page.getTotal(),
                    "content", JsonMessage.optimiseObj(page.getContent(), "title", "id", "introduction", "imageURL", "createDate", "pubdate", "price"));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 视频列表（老师id）
     * @param teacherId
     * @param pageable
     * @return
     */
    @RequestMapping(value = "/vedio_list", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage vedio_list(Long teacherId, Pageable pageable) {
        try {
            Member teacher = memberService.find(teacherId);
            if(teacher == null) {
                return JsonMessage.error("找不到该老师");
            }
            Page<Course> page = courseService.findVedioPageByTeacherId(pageable, teacher);
            return JsonMessage.success(
                    "totalPage", page.getTotalPages(),
                    "pageNumber", page.getPageNumber(),
                    "pageSize", page.getPageSize(),
                    "total", page.getTotal(),
                    "content", JsonMessage.optimiseObj(page.getContent(), "charge","courseType", "title", "id", "encrypt", "password",
                            "introduction", "imageURL", "createDateF", "pubdate", "price", "member.name", "member.avator", "applyCount", "live.watchCount", "live.hisWatchCount", "live.videoURL"));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 视频列表（分类id）
     * @param categoryId
     * @param pageable
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage list(Long categoryId, Pageable pageable) {
        try {
            Category category =  categoryService.find(categoryId);
            if (category == null) {
                return JsonMessage.error("找不到该分类");
            }
            Page<Course> page = courseService.findCoursePage(pageable, category);
            return JsonMessage.success(
                    "totalPage", page.getTotalPages(),
                    "pageNumber", page.getPageNumber(),
                    "pageSize", page.getPageSize(),
                    "total", page.getTotal(),
                    "content", JsonMessage.optimiseObj(page.getContent(), "charge","courseType", "title", "id", "encrypt", "password",
                            "introduction", "imageURL", "createDateF", "pubdate", "price", "member.name", "member.avator", "applyCount",
                            "live.watchCount", "live.hisWatchCount", "live.videoURL"));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 视频中心（直播中）
     * @param pageable
     * @return
     */
    @RequestMapping(value = "/live_list", method = RequestMethod.GET)
    public
    @ResponseBody
    JsonMessage live_list(Pageable pageable) {
        try {
            //获取直播中的视频
            Page<Course> page = courseService.findLivePage(pageable,Course.CourseType.live);
            List<Course> liveCourse = page.getContent();

            return JsonMessage.success(
                    "totalPage", page.getTotalPages(),
                    "pageNumber", page.getPageNumber(),
                    "pageSize", page.getPageSize(),
                    "total", page.getTotal(),
                    "content", JsonMessage.optimiseObj(liveCourse, "applyCount", "live.watchCount", "live.hisWatchCount", "title", "charge", "id", "member.avator", "member.name",
                            "introduction", "imageURL", "createDateF", "pubdate", "price", "encrypt", "password", "courseType"));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }

    /**
     * 首页搜索（直播或视频）
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public
    @ResponseBody
    JsonMessage search(String title, Pageable pageable) {
        try {
            pageable.getFilters().add(Filter.eq("type", Course.Type.course));
            pageable.getFilters().add(Filter.like("title", "%" + title + "%"));
            pageable.getOrders().add(com.hboxs.asl.Order.desc("createDate"));
            Page<Course> page = courseService.findPage(pageable);
            return JsonMessage.success(
                    "totalPage", page.getTotalPages(),
                    "pageNumber", page.getPageNumber(),
                    "pageSize", page.getPageSize(),
                    "total", page.getTotal(),
                    "content", JsonMessage.optimiseObj(page.getContent(), "applyCount", "live.watchCount", "live.hisWatchCount","live.videoURL", "title", "charge", "id", "member.avator", "member.name",
                            "introduction", "imageURL", "createDateF", "pubdate", "price", "encrypt", "password", "type", "courseType"));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }


}
